package com.paradogs.framework.core.events;

import com.paradogs.framework.core.annoations.tick.PRTick;
import com.paradogs.framework.core.annoations.tick.PRTickHolder;
import com.paradogs.framework.core.netty.events.MainLoopEvent;
import com.paradogs.framework.core.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: yumi
 * @date: 2023/7/26  16:30
 * @Description: TODO
 */
@Slf4j
@Component
public class PREventListener {

    @EventListener(MainLoopEvent.class)
    public void onMainLoopEvent(MainLoopEvent event) {
        for (Method method : PRTickHolder.getTickMethods()) {
            try {
                if (method.getParameterCount() == 0) {
                    method.invoke(
                            SpringUtils.getBean(method.getDeclaringClass())
                    );
                } else {
                    method.invoke(
                            SpringUtils.getBean(method.getDeclaringClass()),
                            Stream.of(method.getParameterTypes()).map(aClass -> new Object()).collect(Collectors.toList())
                    );
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
